/**
 * 
 */
package com.vast.vastlcd.views;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;

import com.vast.vastlcd.Activator;
import com.vast.vastlcd.connection.ConnectionManager;
import com.vast.vastlcd.connection.IConnection;
import com.vast.vastlcd.connection.LCDPipeConnection;
import com.vast.vastlcd.connection.LCDSocketConnection;
import com.vast.vastlcd.dialog.SWTConnectionDialog;

/**
 * 
 */
public class ImageView extends ViewPart {

	/**
	 * 
	 */
	//private static Label imageLabel = null;
	private static SWTImageCanvas image_canvas = null;	
	private ConnectionManager com_manager = null;
	
	private Action configureAction = null;
	private Shell shell;
	private SWTConnectionDialog config_con;
	
	/**
	 * 
	 */
	public ImageView() {
		com_manager = new ConnectionManager();
	}

	public ConnectionManager getConnectionManager() {
		return com_manager;
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		this.shell = parent.getShell();
		config_con = new SWTConnectionDialog(shell);
		image_canvas = new SWTImageCanvas(parent);		
		contributeToActionBars();

	}
	
	public static void setImageData(ImageData data) {
		image_canvas.setImageData(data);
	}


	public static void setImage(Image img) {
		image_canvas.setImage(img);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
	}


	
	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalToolBar(bars.getToolBarManager());
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
		configureAction = new Action() {
			public void run() {
				if (config_con.open() == SWTConnectionDialog.OK) {
					
					
					if (config_con.getConnection() != null) {
						getConnectionManager().setConnection(config_con.getConnection());
					}
				}
			}
		};
		configureAction.setImageDescriptor(
				Activator.getDefault().getImageRegistry().getDescriptor("CONFIG"));
		manager.add(configureAction);
		
		com_manager.getStartAction().setImageDescriptor(
				Activator.getDefault().getImageRegistry().getDescriptor("START"));
		manager.add(com_manager.getStartAction());
		com_manager.getStopAction().setImageDescriptor(
				Activator.getDefault().getImageRegistry().getDescriptor("STOP"));
		manager.add(com_manager.getStopAction());
	}
	
	

}
